package com.zzf.concurrent.example.keyword;

import com.zzf.concurrent.example.util.TimeUtil;

/**
 * @author GaoFeng2017
 * @date 2019/8/29 11:01
 */

public class VolatileExample2 {

    private volatile boolean isFinish;
    private Object data;

    public static void main(String[] args) {
        VolatileExample2 volatileExample2 = new VolatileExample2();
        Thread thread1 = new Thread(() -> volatileExample2.init());
        Thread thread2 = new Thread(() -> volatileExample2.work());

        thread2.start();
        thread1.start();

    }

    public void init() {
        System.out.println("start request remote server, the data will be init");
        data = new Object();
        int i= 10;
        isFinish = true;
        System.out.println("finish data init.");
    }

    public void work() {
        while (!isFinish) {
            System.out.println("waiting data init...");
            TimeUtil.sleep(10);
        }

        System.out.println("start working，the data is " + data);
    }

}
